package com.feriaderemates.dao.listing;

import com.feriaderemates.domain.listing.ServiceListing;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class ServiceListingDao extends JdbcDaoSupport implements ServiceListingDaoInterface{

	//Queries
	private static String ADD_SERVICE_LISTING = "INSERT INTO portal.service_listing " +
			" (listing_id,price,variable_price,contact_phone,contact_phone_2,contact_email) VALUES " +
			" (?,?,?,?,?,?)";
	
	private static String UPDATE_SERVICE_LISTING = "UPDATE portal.service_listing SET " +
			" price = ?,variable_price = ?,contact_phone =?,contact_phone_2 =?,contact_email = ? WHERE " +
			" listing_id = ? ";
	
	@Override
	public void addServiceListing(final ServiceListing listing) {
		getJdbcTemplate().update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(ADD_SERVICE_LISTING);
                ps.setLong(1, listing.getListingId());
                ps.setBigDecimal(2, listing.getPrice());
                ps.setBoolean(3, listing.isIs_price_variable());
                ps.setString(4, listing.getContactPhone());
                ps.setString(5, listing.getContactPhone2());
                ps.setString(6, listing.getEmailContact());
                return ps;
            }
        });
    }

	@Override
	public void updateServiceListing(final ServiceListing listing) {
		getJdbcTemplate().update(new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(UPDATE_SERVICE_LISTING);
                ps.setBigDecimal(1, listing.getPrice());
                ps.setBoolean(2, listing.isIs_price_variable());
                ps.setString(3, listing.getContactPhone());
                ps.setString(4, listing.getContactPhone2());
                ps.setString(5, listing.getEmailContact());
                ps.setLong(6, listing.getListingId());
                return ps;
            }
        });
    }
}
